package wk.coral.util.bean;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * description: bean util
 * date 2021-01-16 20:59
 *
 * @author Administrator
 * @version 1.0
 */

@Slf4j
public final class BeanUtil {

    private BeanUtil(){}

    /**
     * transfer object to t instance
     * @param t
     * @param object
     * @param <T>
     */
    public static <T> T bean2Bean(Class<T> t,Object object){
        try {
            Map<String, String> describe = BeanUtils.describe(object);
            T instance = t.newInstance();
            BeanUtils.populate(instance,describe);
            return instance;
        } catch (Exception e) {
            log.error("transfer object to t instance faild",e);
            return null;
        }
    }
}
